/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.liu.modules.business.service.impl;

import com.liu.modules.business.domain.EjlNovelCase;
import com.liu.utils.*;
import lombok.RequiredArgsConstructor;
import com.liu.modules.business.repository.EjlNovelCaseRepository;
import com.liu.modules.business.service.EjlNovelCaseService;
import com.liu.modules.business.service.dto.EjlNovelCaseDto;
import com.liu.modules.business.service.dto.EjlNovelCaseQueryCriteria;
import com.liu.modules.business.service.mapstruct.EjlNovelCaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Liu
* @date 2022-07-07
**/
@Service
@RequiredArgsConstructor
public class EjlNovelCaseServiceImpl implements EjlNovelCaseService {

    private final EjlNovelCaseRepository ejlNovelCaseRepository;
    private final EjlNovelCaseMapper ejlNovelCaseMapper;

    @Override
    public Map<String,Object> queryAll(EjlNovelCaseQueryCriteria criteria, Pageable pageable){
        Page<EjlNovelCase> page = ejlNovelCaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ejlNovelCaseMapper::toDto));
    }

    @Override
    public List<EjlNovelCaseDto> queryAll(EjlNovelCaseQueryCriteria criteria){
        return ejlNovelCaseMapper.toDto(ejlNovelCaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public EjlNovelCaseDto findById(String novelId) {
        EjlNovelCase ejlNovelCase = ejlNovelCaseRepository.findById(novelId).orElseGet(EjlNovelCase::new);
        ValidationUtil.isNull(ejlNovelCase.getNovelId(),"EjlNovelCase","novelId",novelId);
        return ejlNovelCaseMapper.toDto(ejlNovelCase);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public EjlNovelCaseDto create(EjlNovelCase resources) {
        return ejlNovelCaseMapper.toDto(ejlNovelCaseRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(EjlNovelCase resources) {
        EjlNovelCase ejlNovelCase = ejlNovelCaseRepository.findById(resources.getNovelId()).orElseGet(EjlNovelCase::new);
        ValidationUtil.isNull( ejlNovelCase.getNovelId(),"EjlNovelCase","id",resources.getNovelId());
        ejlNovelCase.copy(resources);
        ejlNovelCaseRepository.save(ejlNovelCase);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String novelId : ids) {
            ejlNovelCaseRepository.deleteById(novelId);
        }
    }

    @Override
    public void deleteByNovelAndUser(String userId, String novelId) {
        ejlNovelCaseRepository.deleteByNovelAndUser(userId,novelId);
    }

    @Override
    public void download(List<EjlNovelCaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EjlNovelCaseDto ejlNovelCase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建日期", ejlNovelCase.getCreateTime());
            map.put("更新日期", ejlNovelCase.getUpdateTime());
            map.put("章节索引", ejlNovelCase.getChapterIndex());
            map.put("小说名称", ejlNovelCase.getNovelName());
            map.put("章节名称", ejlNovelCase.getChapterTitle());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}